package com.saturn.titan.web.controller;

import java.text.ParseException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.saturn.titan.core.bean.BusiDeployBill;
import com.saturn.titan.core.bean.BusiWorkBill;
import com.saturn.titan.core.comm.Constants;
import com.saturn.titan.core.service.DeployBillService;
import com.saturn.titan.core.service.WorkBillService;

/**
 * @author liuwenhao
 */
@Controller
@RequestMapping(value = "/deployBill")
public class DeployBillController extends BaseController{

	@Autowired
	private WorkBillService workBillService;
	
	@Autowired
	private DeployBillService deployBillService;
	
	@RequestMapping(value = "selectDeployEmp", method = RequestMethod.POST)
	public String selectDeployEmp(@RequestParam(value="workBillId") Long id,Model model) throws ParseException {
		BusiWorkBill workBill = workBillService.findById(id);
		if(workBill == null){
			model.addAttribute("error","指定工单不存在，不能分派");
		}else if(workBill.getBillStt().intValue() != 0 && workBill.getBillStt().intValue() != 4){
			model.addAttribute("error","工单："+workBill.getBillNo()+" 不是待分派状态，不能分派");
		}else{
			model.addAttribute("workbill", workBill);
		}
		
		return "deployBill/deploy";
	}
	
	@RequestMapping(value = "deploy", method = RequestMethod.POST)
	public String deploy(@RequestParam(value="empId",required=false) Long empId,
			@RequestParam(value="dealerId",required=false) Long dealerId,
			@RequestParam(value="workBillId")Long workBillId,
			Model model) throws ParseException {
		BusiWorkBill workBill = workBillService.findById(workBillId);
		model.addAttribute("workbill", workBill);
		
		if(empId != null){
			deployBillService.deployEmpWorkBill(empId,workBillId);
		}else{
			deployBillService.deployDealerWorkBill(dealerId,workBillId);
		}
		
		
		if(Constants.REQUEST_TYPE_REPARE.equals(workBill.getRequestType())){
			return "repareBill/viewRepare";
		}else if(Constants.REQUEST_TYPE_INSTALL.equals(workBill.getRequestType())){
			return "installBill/viewInstall";
		}
		
		return "";
	}
}
